// pages/me/setting.js
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    fontSize: 16,
    isSubscribe: false,
    canIUseGetUserProfile: false,
    userInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var wx_userInfo = wx.getStorageSync("wx_userInfo");
    that.setData({
      userInfo: wx_userInfo
    })

    if (wx.canIUse('getUserProfile')) {
      that.setData({
        canIUseGetUserProfile: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var fontSize = wx.getStorageSync("fontSize");

    if (fontSize) {
      this.setData({
        fontSize: fontSize,
      });
    }

    // var isSubscribe = wx.getStorageSync("isSubscribe");
    // if (isSubscribe){
    //   this.setData({
    //     isSubscribe: true
    //   })
    // }

    this.doBinding();
  },
  fontSizeTap: function () {
    var that = this;
    var fontSizeArray = ["14", "16", "18", "20", "22", "24"];
    wx.showActionSheet({
      itemList: fontSizeArray,
      success(res) {
        var fontSize = fontSizeArray[res.tapIndex];
        console.log(fontSize)
        wx.setStorageSync("fontSize", fontSize);
        that.setData({
          fontSize: fontSize,
        });
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  authLogin: function (e) {
    console.log(e.detail.userInfo)
    if (e.detail.userInfo) {
      this.getUserInfo();
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    var that = this;
    wx.getUserProfile({
      desc: '用于完善用户资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        var userInfo = res.userInfo;
        console.log('userInfo', userInfo);

        //更新数据
        that.setData({
          userInfo: userInfo
        })
        wx.setStorageSync("wx_userInfo", userInfo);

        var openId = wx.getStorageSync("openId");
        if (!openId) { //检查本地的openId
          wx.cloud.callFunction({
            name: 'getOpenData',
            complete: res => {
              var openId = res.result.userInfo.openId;
              console.log('callFunction result: ', res.result);
              wx.setStorageSync("openId", openId);
              that.doBinding();
            }
          })
        }
      }
    })
  },
  getUserInfo: function () {
    var that = this;
    //调用登录接口
    wx.getUserInfo({
      withCredentials: false,
      success: function (res) {
        var userInfo = res.userInfo;
        console.log('userInfo', userInfo);

        //更新数据
        that.setData({
          userInfo: userInfo
        })
        wx.setStorageSync("wx_userInfo", userInfo);

        var openId = wx.getStorageSync("openId");
        if (!openId) { //检查本地的openId
          wx.cloud.callFunction({
            name: 'getOpenData',
            complete: res => {
              var openId = res.result.userInfo.openId;
              console.log('callFunction result: ', res.result);
              wx.setStorageSync("openId", openId);
              that.doBinding();
            }
          })
        }
      }
    })
  },
  doBinding: function () {
    var formData = {
      "platform": 4
    };

    var latitude = wx.getStorageSync("latitude");
    var longitude = wx.getStorageSync("longitude");
    var openId = wx.getStorageSync("openId");
    if (latitude) {
      formData["latitude"] = latitude;
    }
    if (longitude) {
      formData["longitude"] = longitude;
    }
    if (openId) {
      formData["openid"] = openId;
    }
    http.ApiPost(apiUrl.UrlBinding, formData, function (res) {

      if (res.code == 1) {
        console.log(res.msg);
      }
    });
  },
  subscribeTap: function () {
    var that = this;
    wx.getSetting({
      success(res) {
        console.log(res)
        if (res.subscriptionsSetting) {
          wx.openSetting({
            success(res) {
              console.log(res.authSetting)
            }
          })
        } else {
          wx.requestSubscribeMessage({
            tmplIds: [util.tmplID],
            success(res) {
              console.log(res);
              // wx.setStorageSync("isSubscribe", true);
              // that.setData({
              //   isSubscribe: true
              // })
            }
          })
        }
      }
    })
  },
})